//
//  libQPJHLightChannel.m
//  libQPJHLightChannel
//
//  Created by qply on 2020/9/8.
//  Copyright © 2020 qipa. All rights reserved.
//

#import "libQPJHLightChannel.h"
#import "YZYSDKManager.h"
#import "QPJHChannelRoleInfo.h"
#import "QPJHChannelOrderInfo.h"

@interface libQPJHLightChannel ()<YZYSDKManagerDelegate>
@property (nonatomic, strong) UIWindow *keyWindow;

@end

@implementation libQPJHLightChannel
+ (libQPJHLightChannel *)sharedInstance {
    static libQPJHLightChannel *sharedManagerInstance = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        sharedManagerInstance = [[self alloc] init];
        
    });
    return sharedManagerInstance;
}
- (NSString *)platformId {
    return @"40048";
}
- (NSString *)loginUrl {
    return @"https://supersdk.7pa.com/login/index";
}
- (NSString *)orderUrl {
    return @"https://supersdk.7pa.com/order/index";
}

/**
 设置监听
 */
- (void)qpjhChannel_setStartLoginListener : (id)loginListener {
    NSLog(@"yzysdk - qpjhChannel_setStartLoginListener");
    [libQPJHLightChannel sharedInstance].delegate = loginListener;
    [[YZYSDKManager shareManager] yzy_sdkManagerListener:self];
}

/**
 登录
 */
- (void)qpjhChannel_lightLoginStart {
    NSLog(@"yzysdk - qpjhChannel_lightLoginStart");
    [[YZYSDKManager shareManager] yzy_showLoginUI];
}

/**
 提交角色信息(在 创建角色、 进入游戏、 升级角色 时都要调用该方法.)
 */
- (void)qpjhChannel_superRoleUpdate:(QPJHChannelRoleInfo *)roleInfo {
    NSLog(@"yzysdk - roleId:%@ roleName:%@ roleLevel:%@ serviceId:%@ serviceName:%@ powerValue:%@ params:%@ responseObject:%@", roleInfo.role_id, roleInfo.role_name, roleInfo.role_level, roleInfo.service_id, roleInfo.service_name, roleInfo.power_value, roleInfo.params, roleInfo.responseObject);

    // 更新角色信息
    /*
     UploadRoleInfoTypeNone = 1,     //未定义
     UploadRoleInfoTypeEnterGame,    //进入游戏
     UploadRoleInfoTypeCreateRole,   //创建角色
     UploadRoleInfoTypeRoleLevelUp,  //角色升级
     UploadRoleInfoTypeJoinParty,    //加入帮派
     UploadRoleInfoTypeExitGame,     //退出游戏
     UploadRoleInfoTypeOther         //其他情况
     */
    NSString *role_type = @"enterserver";
    if ([roleInfo.params.allKeys containsObject:@"role_type"]) {
        role_type = roleInfo.params[@"role_type"];
    }
    NSString *describe = @"1";
    if ([roleInfo.params.allKeys containsObject:@"describe"]) {
        describe = roleInfo.params[@"describe"];
    }
    NSString *experience = @"1";
    if ([roleInfo.params.allKeys containsObject:@"experience"]) {
        experience = roleInfo.params[@"experience"];
    }
    NSString *money = @"1";
    if ([roleInfo.params.allKeys containsObject:@"money"]) {
        money = roleInfo.params[@"money"];
    }
    NSString *role_id = @"0";
    if ([roleInfo.params.allKeys containsObject:@"role_id"]) {
        role_id = roleInfo.params[@"role_id"];
    }
    NSString *super_user_id = @"0";
    if ([roleInfo.params.allKeys containsObject:@"super_user_id"]) {
        super_user_id = roleInfo.params[@"super_user_id"];
    }
    
    YZYRoleInfo *yzy_roleInfo = [[YZYRoleInfo alloc] init];
    yzy_roleInfo.role_type = role_type;//1进入游戏 2退出游戏 3创建角色 4更新角色
    yzy_roleInfo.super_role_id = roleInfo.role_id;//这里的角色id是奇葩平台角色id
    yzy_roleInfo.role_id = role_id;//这里的角色id是游戏角色id
    yzy_roleInfo.super_user_id = super_user_id;
    yzy_roleInfo.role_level = roleInfo.role_level;
    yzy_roleInfo.role_name = roleInfo.role_name;
    yzy_roleInfo.service_id = roleInfo.service_id;
    yzy_roleInfo.service_name = roleInfo.service_name;
    yzy_roleInfo.describe = describe;
    yzy_roleInfo.experience = experience;
    yzy_roleInfo.money = money;
    [[YZYSDKManager shareManager] yzy_submitRoleInfo:yzy_roleInfo];
}

/**
 创建订单接口
 */
- (void)qpjhChannel_superOrder:(QPJHChannelOrderInfo *)orderInfo {    
    [[YZYSDKManager shareManager] yzy_applePayWithGoodID:orderInfo.goodID PayMoney:orderInfo.payMoney gameOrderSn:orderInfo.gameOrderSn remark:orderInfo.remark];
}

/**
 退出登录接口
 */
- (void)qpjhChannel_logout {
    NSLog(@"yzysdk - qpjhChannel_logout");
    [[YZYSDKManager shareManager] yzy_logoutPlatform];
}
/**
 跳转小程序
 */
- (void)qpjhChannel_jump_miniPrograms {
    NSLog(@"yzysdk - qpjhChannel_jump_miniPrograms");
    [[YZYSDKManager shareManager] yzy_jump_miniPrograms];
    
}
/**
 账号删除接口
 */
- (void)qpjhChannel_logoutForAccountDelete {
    [[YZYSDKManager shareManager] yzy_logoutPlatformForAccountDelete];
}


#pragma mark - YZYSDKManagerDelegate

/**
 登录回调
 loginResult 0取消 1成功 2失败
 成功时data为
 @{
 @"super_user_id": self.userId,
 @"token": accessToken
};
 */
- (void)yzy_LoginCallBack:(NSInteger)loginResult data:(id)data {
    NSLog(@"yzysdk - loginResult - %ld", loginResult);
    if (loginResult == 1) {
        if ([self.delegate respondsToSelector:@selector(qpjhChannel_LightGetChannelLoginCallBack:)]) {
            [self.delegate qpjhChannel_LightGetChannelLoginCallBack:data];
        }
    }
    
}

/**
 切换/退出账号回调
 此回调表示平台已完成退出，请勿再调用平台退出接口，否则会造成死循环；
 请在此方法进行游戏退出操作
 */
- (void)yzy_appleChangeAccountOrLogoutCallBack:(NSString *)result {
    NSLog(@"yzysdk - 注销登录成功");
    
    if ([self.delegate respondsToSelector:@selector(qpjhChannel_logOutCallBack:)]) {
        [self.delegate qpjhChannel_logOutCallBack:result];
    }
}

/**
 支付回调
 payResult 0取消 1成功 2失败
 */
- (void)yzy_applePayCallBack:(NSInteger)payResult data:(id)data {
    if (payResult == 0) {
        NSLog(@"yzysdk - SDK 支付取消");
        if ([self.delegate respondsToSelector:@selector(qpjhChannel_getPayCancelCallBack:)]) {
            [self.delegate qpjhChannel_getPayCancelCallBack:data];
        }
    } else if (payResult == 1) {
		NSLog(@"yzysdk - 支付成功");
        if ([self.delegate respondsToSelector:@selector(qpjhChannel_getPaySuccessCallBack:)]) {
            [self.delegate qpjhChannel_getPaySuccessCallBack:data];
        }
    } else {
        NSLog(@"yzysdk - 支付失败");
        if ([self.delegate respondsToSelector:@selector(qpjhChannel_getPayFailCallBack:)]) {
            [self.delegate qpjhChannel_getPayFailCallBack:data];
        }
    }
}

/**
 上报角色回调
 */
- (void)yzy_updateRoleCallBack:(NSDictionary *)result {
    NSLog(@"yzysdk - 上传角色结果----result=%@",result);
    if ([self.delegate respondsToSelector:@selector(qpjhChannel_getUpdateRoleCallBack:)]) {
        [self.delegate qpjhChannel_getUpdateRoleCallBack:result];
    }
}

/**
 在有网络后的一些初始化操作
 */
- (void)qpjhAfterNerwork_init {
    NSLog(@"yzysdk - qpjhAfterNerwork_init");
    [[YZYSDKManager shareManager] yzy_AfterNerwork_init];
}

#pragma mark - UIApplicationDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    [[YZYSDKManager shareManager] application:application didFinishLaunchingWithOptions:launchOptions];
    return YES;
}

- (void)applicationWillResignActive:(UIApplication *)application {
    [[YZYSDKManager shareManager] applicationWillResignActive:application];
}
- (void)applicationDidEnterBackground:(UIApplication *)application {
    [[YZYSDKManager shareManager] applicationDidEnterBackground:application];
}
- (void)applicationWillEnterForeground:(UIApplication *)application {
    [[YZYSDKManager shareManager] applicationWillEnterForeground:application];
}
- (void)applicationDidBecomeActive:(UIApplication*)application
{
    [[YZYSDKManager shareManager] applicationDidBecomeActive:application];
}
- (void)applicationWillTerminate:(UIApplication *)application {
    [[YZYSDKManager shareManager] applicationWillTerminate:application];
}
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
    [[YZYSDKManager shareManager] application:application didRegisterForRemoteNotificationsWithDeviceToken:deviceToken];
}
- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {
    [[YZYSDKManager shareManager] application:application didFailToRegisterForRemoteNotificationsWithError:error];
}
- (UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window {
    return [[YZYSDKManager shareManager] application:application supportedInterfaceOrientationsForWindow:window];
}
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(nonnull NSDictionary *)userInfo {
    [[YZYSDKManager shareManager] application:application didReceiveRemoteNotification:userInfo];
}
- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<UIApplicationOpenURLOptionsKey,id> *)options {
    return [[YZYSDKManager shareManager] application:app openURL:url options:options];
}
- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url {
    return [[YZYSDKManager shareManager] application:application handleOpenURL:url];
}
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation {
    return [[YZYSDKManager shareManager] application:application openURL:url sourceApplication:sourceApplication annotation:annotation];
}
- (BOOL)application:(UIApplication *)application continueUserActivity:(NSUserActivity *)userActivity restorationHandler:(void(^)(NSArray * restorableObjects))restorationHandler {
    return [[YZYSDKManager shareManager] application:application continueUserActivity:userActivity restorationHandler:restorationHandler];
}


@end
